package com.yanqu.road.dao.impl.activity.coupon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.coupon.CouponActivityUserData;
import com.yanqu.road.entity.activity.coupon.CouponTrigger;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class CouponUserDataDaoImpl extends TempDataObjectDao<CouponActivityUserData> {

    public CouponActivityUserData getCouponActivityUserData(int activityId, long userId) {
        String sql = "select * from `t_u_activity_coupon_user_data` where `activityId`=? and `userId`=?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, activityId);
            call.setLong(2, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean updateData(CouponActivityUserData data) {
        String sql = "update `t_u_activity_coupon_user_data` set triggerData=? where activityId=? and userId=?";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getCouponTriggerMapJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean addData(CouponActivityUserData data) {
        String sql = "insert into `t_u_activity_ad_user_data`(activityId,userId,triggerData) values (?,?,?)";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getCouponTriggerMapJsonStr()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CouponActivityUserData parseData(ResultSet rs) throws SQLException {
        CouponActivityUserData data = new CouponActivityUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        String value = rs.getString("triggerData");
        if(!StringUtils.isNullOrEmpty(value)){
            Map<Integer, CouponTrigger> couponTriggerMap = JSON.parseObject(value, new TypeReference<Map<Integer, CouponTrigger>>() {});
            data.setCouponTriggerMap(couponTriggerMap);
        }
        return data;
    }
}
